package vacuum.lgadmin.performance;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import vacuum.lgadmin.LGAdmin;
import vacuum.lgadmin.LGPlugin;

public class PerformancePlugin extends LGPlugin
{
	//TODO conver commands to new system
	public PerformancePlugin(LGAdmin lga)
	{
		super(lga);
	}

	@Override
	public void onEnable()
	{
		
		lga.getCommand("ram").setExecutor(new CommandExecutor()
		{
			
			@Override
			public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
					String[] arg3)
			{
				if(arg3.length > 0){
					arg0.sendMessage(ChatColor.RED + "Too many arguments.");
					return true;
				}
				arg0.sendMessage(ChatColor.BLUE + "[LGPerformance] Free memory: " + (Runtime.getRuntime().freeMemory() / 1048576) + "MB.");
				arg0.sendMessage(ChatColor.BLUE + "[LGPerformance] Maximum memory: " + (Runtime.getRuntime().maxMemory() / 1048576) + "MB.");
				arg0.sendMessage(ChatColor.BLUE + "[LGPerformance] Total memory: " + (Runtime.getRuntime().totalMemory() / 1048576) + "MB.");
				arg0.sendMessage(ChatColor.BLUE + "[LGPerformance] Type /gc to force garbage collection.");
				return true;
			}
		});
		
		lga.getCommand("gc").setExecutor(new CommandExecutor()
		{
			
			@Override
			public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
					String[] arg3)
			{
				long before = Runtime.getRuntime().freeMemory();
				if(arg3.length > 0){
					arg0.sendMessage(ChatColor.RED + "Too many arguments");
					return true;
				}
				arg0.sendMessage(ChatColor.BLUE + "[LGPerformance] Initializing garbage collection...");
				if(arg0 instanceof Player){
					System.out.println("[LGPerformance] " + arg0.getName() + " started a garbabe collection.");
				}
				Runtime.getRuntime().gc();
				arg0.sendMessage(ChatColor.GREEN + "[LGPerformance] Garbage collection finished. Collected " + ((Runtime.getRuntime().freeMemory() - before) / 1048576) + "MB.");
				return true;
			}
		});
	}

	@Override
	public void onDisable()
	{
		lga.getCommand("ram").setExecutor(null);
		lga.getCommand("gc").setExecutor(null);
	}

	@Override
	public String getName()
	{
		return "Performance";
	}

	@Override
	public String getVersion()
	{
		return "1.0.0";
	}

}
